import type { Request, ResponseToolkit } from "@hapi/hapi";
import Hapi from "@hapi/hapi";
import { User, NoteCate } from "../../model/index";

let route: Hapi.ServerRoute<Hapi.ReqRefDefaults> = {
  path: "/getNoteList",
  method: "get",
  handler: async (r: any, h: ResponseToolkit) => {
    try {
      console.log(r.userId);
      const cateList: any = await NoteCate.find();
      const list: any = await User.findById(r.userId)
        .select("note")
        .populate("note")
        .select("pid");
      const data = list.note.map((item: any) => {
        let o = cateList.find((res: any) => item.type == res.type);
        item._doc.cateObj = o;
        if (o) {
          return item;
        }
      });
      return { code: 200, data: data };
    } catch (err: any) {
      return err;
    }
  },
  options: {
    plugins: {
      "hapi-swagger": {
        security: [{ apiKey: [] }],
      },
    },
    tags: ["api"],
  },
};
export default route;
